﻿/*
 * Criado por SharpDevelop.
 * Usuário: Renato
 * Data: 30/9/2008
 * Hora: 11:47
 * 
 * Para alterar este modelo use Ferramentas | Opções | Codificação | Editar Cabeçalhos Padrão.
 */

using Gtk;
using System;
using asdf.consultacotacao.riscozeroprojetos.com.br;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace dotCotaAcoes
{
	public class MainWindow : Window
	{
		Entry e;
		ListStore acoes;
		Button b;
		List<String> listaAcoes;
		
		void VerificaCotacoes()
		{
			CotacaoService cs = new CotacaoService();
			string a = 	listaAcoes[0];
			listaAcoes.RemoveAt(0);
			Console.WriteLine("item removido: {0}",a);
			DadosCotacao d = cs.ObterCotacao(a);
			
			acoes.AppendValues(d.codAtivo,
			                   d.valAbertura.ToString(),
			                   d.valAtual.ToString(),
			                   d.qtdVariacao.ToString(),
			                   d.horCotacao.ToString());
			b.Label = "Atualizar";
		}
		
		void click (object obj, EventArgs args)
		{
			
			string aPath = Environment.CurrentDirectory + "\\acoes.txt";
			if (!File.Exists(aPath))
			{
				MessageDialog md = new MessageDialog (this,
				                                      DialogFlags.DestroyWithParent,
				                                      MessageType.Warning,
				                                      ButtonsType.Close,
				                                      "Erro ao carregar arquivo: "+aPath);
				
				int result = md.Run ();
				md.Destroy();

				return;
			}
			
			
			//FileStream fs = new FileStream(aPath,FileMode.Open);
			StreamReader sr = new StreamReader(aPath);
			listaAcoes = new List<string>();
			string s;
			while ((s = sr.ReadLine())!= null)
			{
				listaAcoes.Add(s);
				Console.WriteLine(s);							
			}
			acoes.Clear();
			
			Button b = (Button) obj;
			b.Label = "Atualizando...";
			do{
				VerificaCotacoes();
				Console.WriteLine("Atualizando...{0}",listaAcoes.Count.ToString());
			}while(!listaAcoes.Count.Equals(0));
			Console.Write("Concluido");
			
			
		}
		public MainWindow() : base("MainWindow")
		{
			
			this.SetDefaultSize(200,200);
			DeleteEvent += new DeleteEventHandler(MainWindowDeleteEvent);
			VBox vb = new VBox();
			HBox hb = new HBox();
			HBox hb1 = new HBox();
			vb.Add(hb);
			vb.Add(hb1);

			TreeView tv = new TreeView();
			tv.SetUsize(300,200);
			TreeViewColumn Papel =  new TreeViewColumn("Papel",new CellRendererText(),"text",0);
			TreeViewColumn Abertura =  new TreeViewColumn("Abertura",new CellRendererText(),"text",1);
			TreeViewColumn ValorAtual =  new TreeViewColumn("Valor Atual",new CellRendererText(),"text",2);
			TreeViewColumn Variacao =  new TreeViewColumn("Variação",new CellRendererText(),"text",3);
			TreeViewColumn Hora =  new TreeViewColumn("Hora",new CellRendererText(),"text",4);
			
			
			
			tv.AppendColumn(Papel);
			tv.AppendColumn(Abertura);
			tv.AppendColumn(ValorAtual);
			tv.AppendColumn(Variacao);
			tv.AppendColumn(Hora);
			
			acoes = new ListStore(typeof(string),
			                      typeof(string),
			                      typeof(string),
			                      typeof(string),
			                      typeof(string)
			                     );
			tv.Model = acoes;
			
			
			hb1.Add(tv);
			
			b = new Button("Atualizar");
			
			b.Clicked += new EventHandler(click);
			b.SetSizeRequest(150,24);
			hb.Add(b);
			
			this.Add(vb);
			ShowAll();
		}
		
		[STAThread]
		public static void Main(string[] arg)
		{
			Application.Init();
			new MainWindow();
			Application.Run();
		}
		
		void MainWindowDeleteEvent(object o, DeleteEventArgs args)
		{
			Application.Quit();
			args.RetVal = true;
		}
	}
}
